﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using CommunityToolkit.Mvvm.ComponentModel;
using SpCad;
using SPCAM.Plugin.Cam.Enums;
using SPCAM.Plugin.Cam.Model;
using SPCAM.Plugin.Cam.Tools;
using SPCAM.Servers;

namespace SPCAM.Plugin.Cam.ViewModel
{
    public class CamViewModelBase: ObservableObject
    {
        private IServer? _Sv;
        private SelectFcaeTypeEnum _SelctFaceType;
        public IServer Sv { get=>_Sv??CamPlugin.Instance.server; set=> _Sv = value; }
        public SelectShapeModel? SelectToolsModel { get; set; }
        public SelectFcaeTypeEnum SelctFaceType { get => _SelctFaceType; set => SetProperty(ref _SelctFaceType, value); }
        public CamViewModelBase()
        {
            SpServer.Instance.PropertyChanged += Instance_PropertyChanged;
        }

        private void Instance_PropertyChanged(object? arg1, System.ComponentModel.PropertyChangedEventArgs arg2)
        {
          if(arg2.PropertyName== "CurrentEditor")
            {
                SpServer.Instance.RemoveHintMessage();
            }
        }

        public virtual void ClearSelect()
        {
            CamPlugin.Instance.MainHander.ClearItem();
            SpServer.Instance.WorkspaceController.CurrentTool?.Stop();
            SelectToolsModel=null;
        }
        public virtual void SelectShape(SelectShapeModel mod)
        {
            if (mod.tool == null)
            {
                SpServer.Instance.SetHintMessage("请线选择实体");
                ClearSelect();
                return;
            }
            SelectToolsModel = mod;
            CamPlugin.Instance.MainHander.ShowItem(mod.item);
        }
        /// <summary>
        /// 执行工具
        /// </summary>
        public virtual void DoTools()
        {
            if (SelectToolsModel != null)
            {

                //  Server.Instance.IsCamTabActive = true;
                CamPlugin.Instance.server.DokingWindow.ActivateToolAnchorable("cam");
                SpServer.Instance.Commit();
                SpServer.Instance.StartTools(SelectToolsModel.tool,(s,e)=>
                {
                    var @tools = s as SelectFasceTools;
                    if (tools != null)
                    {
                        if (!tools.IsNoCanExit)
                        {
                           ClearSelect();
                        }

                    }
                }
                );
            }
        }
    }
}
